package ezXpns.GUI;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import net.miginfocom.swing.MigLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;

import ezXpns.GUI.controllers.BudgetChangeController;
import ezXpns.GUI.controllers.RequestResult;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;


/**
 * @author A0086931X
 */

//@author A0086931X
public class ChangeTypeGUI extends JDialog {
	/**
	 * Create the dialog.
	 */
	public ChangeTypeGUI(BudgetChangePanel parent) {
		this.parent = parent;
		setTitle("Edit Types");
		typeList = new DefaultListModel<String>();

		setBounds(100, 100, 245, 211);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new MigLayout("", "[80][300,grow,fill][80]", "[123.00px,grow][]"));
		{
			list = new JList<String>(typeList);
			list.setVisibleRowCount(6);
			list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

			JScrollPane scrollPane = new JScrollPane(list);
			contentPanel.add(scrollPane, "cell 1 0,alignx left,aligny top");
		}
		{
			txtAddTag = new JTextField();
			txtAddTag.addFocusListener(new FocusAdapter() {
				@Override
				public void focusGained(FocusEvent e) {
					if(txtAddTag.equals("add type ")){
						txtAddTag.setText("");
					}
				}
				
				@Override
				public void focusLost(FocusEvent e) {
					if(txtAddTag.equals("")){
						txtAddTag.setText("add type ");
					}
				}
			});
			txtAddTag.setText("add type ");
			contentPanel.add(txtAddTag, "cell 1 1,growx");
			txtAddTag.setColumns(10);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton btnAddTag = new JButton("Add");
				btnAddTag.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						addType();
					}
				});
				buttonPane.add(btnAddTag);
			}
			{
				JButton btnEdit = new JButton("Edit");
				btnEdit.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						editType();
					}
				});
				buttonPane.add(btnEdit);
			}
			{
				JButton btnDeleteTag = new JButton("Delete");
				btnDeleteTag.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						removeType();
					}
				});
				buttonPane.add(btnDeleteTag);
			}
		}		
	}
	
	public void connect(BudgetChangeController controller) {
		if(this.controller != null) return;		
		this.controller = controller;
	}

	public void setAvailableTypes(String[] types) {
		typeList.clear();
		for (String type : types) {
			if(!type.equals("Other"))//exclude this special type
				typeList.addElement(type);
		}
	}
	
	private void addType() {
		String newType = txtAddTag.getText().trim();
		handleResult(
			controller.requestAddType(newType)
		);
	}

	private void editType() {
		String newType = txtAddTag.getText().trim();
		handleResult(
			controller.requestEditType(list.getSelectedValue(), newType)
		);
	}

	private void removeType() {
		String removedType = list.getSelectedValue();
		if(removedType != null) {
			handleResult(
				controller.requestRemoveType(removedType)
			);	
		}
	}

	private void handleResult(RequestResult result) {		
		if(!result.isAccepted()) {
			System.out.println("Error: " + result.getError());
			//showError(result.getError());
		}
		else {
			parent.updateUndoRedoStatus();
		}
	}	

	private static final long serialVersionUID = 1L;

	private BudgetChangeController controller;
	private DefaultListModel<String> typeList;
	private final JPanel contentPanel = new JPanel();
	private JTextField txtAddTag;
	private JList<String> list;
	private BudgetChangePanel parent;
}
